package com.me.libgdx_test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;

public class ClassCharacterEnemy extends AbstractClassCharacter{
	ClassDungeonEnemy enemy_info;
	String level_image;
	Random random_gen = new Random(System.currentTimeMillis());
	String enemy_name;
	/*mods[1] = mod_MRplus;
	mods[2] = mod_PRnorm;
	mods[3] = mod_PRplus;
	mods[4] = mod_magicalAttack;
	mods[5] = mod_undead;
	mods[6] = mod_fStrike;
	ailments[0] = ail_mburn;
	ailments[1] = ail_poison;
	ailments[2] = ail_forget;
	ailments[3] = ail_blind;
	ailments[4] = ail_weakness;
	ailments[5] = ail_static;
	*/
	ClassCharacterEnemy(ClassCharacterEnemy enemy){
		this.attack = enemy.attack;
		this.max_health = enemy.max_health;
		this.current_health = enemy.current_health;
		this.lives = enemy.lives;
		this.enemy_info = enemy.enemy_info;
		enemy_name = "";
		
	}
	ClassCharacterEnemy(ClassDungeonEnemy enemy_info){
		this.max_health = enemy_info.base_stats[0];
		this.current_health = enemy_info.base_stats[0];
		this.attack = enemy_info.base_stats[1];
		this.lives = enemy_info.base_stats[2];
		this.enemy_info = enemy_info;
		enemy_name = "";
	}
	public String get_name(){
		return enemy_name;
	}
	public void set_modifiers(ClassDungeonMods[] info_mods){
		
		//mburn, poison, MRnorm, MRplus, PRnorm, PRplus, forget, blind, weakness, magicalAttack, undead, coward, banish, fStrike, static
		ClassLogDisplay logger = new ClassLogDisplay("ClassCharacterEnemy");
		//logger.print("Enemy Mods");
		ClassCharacterModDungeon[] dungeon_mods = new ClassCharacterModDungeon[info_mods.length];
		for(int i = 0; i<enemy_info.ability_stats.length;i++){
			dungeon_mods[i] = new ClassCharacterModDungeon(info_mods[i],enemy_info.ability_stats[i]);
		}
		//Blood and extra life mods. not applicable for enemies
		dungeon_mods[enemy_info.ability_stats.length] = new ClassCharacterModDungeon(info_mods[enemy_info.ability_stats.length],false);
		dungeon_mods[enemy_info.ability_stats.length+1] = new ClassCharacterModDungeon(info_mods[enemy_info.ability_stats.length+1],false);
		this.set_mods(dungeon_mods);
		/*this.set_ailments(enemy_info.ability_stats[0], enemy_info.ability_stats[1]?1:0, 
				enemy_info.ability_stats[6]?1:0, enemy_info.ability_stats[7]?1:0, enemy_info.ability_stats[8]?1:0, 
				enemy_info.ability_stats[14]?1:0);
		this.set_mods(enemy_info.ability_stats[2]?1:0, enemy_info.ability_stats[3]?1:0, 
				enemy_info.ability_stats[4]?1:0, enemy_info.ability_stats[5]?1:0, enemy_info.ability_stats[9]?1:0, 
				enemy_info.ability_stats[10]?1:0,enemy_info.ability_stats[11]?1:0,enemy_info.ability_stats[12]?1:0,
				enemy_info.ability_stats[13]?1:0,0);*/
	}
	public void add_level(){
		int health_increase = max_health / level;
		int attack_increase = attack / level;
		if(level!=10){
			level++;
			assign_level_image();
			this.max_health += health_increase;
			this.attack += attack_increase;		
			this.current_health = this.max_health;
		}
	}
	public int roll(int total){
		return random_gen.nextInt(total);
	}
	public void update_enemy_stats(float difficulty){
		this.max_health *= ((float)this.level * difficulty);
		max_health = max_health < 1?1:max_health;
		this.attack *= ((float)this.level * difficulty);
		attack = attack < 1?1:attack;
		if(enemy_info.Name == "Re-Animate"){
			lives = level;
		}
		if(level == 10){
			enemy_name = enemy_info.boss_name;
			//Give the boss more health. Just because it's too easy without it
			max_health *= 1.5;
		}else{
			enemy_name = enemy_info.Name + " " + enemy_info.first_names[roll(enemy_info.first_names.length-1)];
		}
		this.current_health = this.max_health;
	}
	
	public int predict_spell(int spell_attack,float mod){
		int current_spell_attack = spell_attack; 
		float spell_strength = mod;
		if(get_contracted_mod("+25% Magic Resistance") || get_attack_mod("+25% Magic Resistance")){
			spell_strength-=.25f;
		}
		if(get_contracted_mod("+50% Magic Resistance") || get_attack_mod("+50% Magic Resistance")){
			spell_strength-=.50f;
		}
		current_spell_attack = (int)((float)current_spell_attack * spell_strength);
		current_spell_attack = current_spell_attack < 1?1:current_spell_attack;

		return current_spell_attack;
	}
	
	public int attack_spell(int spell_attack,float mod){
		current_health -= predict_spell(spell_attack, mod);
		
		check_for_death();
		return this.current_health;
		
	}
	public void check_for_death(){
		if(this.lives>1 && this.current_health<1){
			this.current_health = 1;
			this.lives--;
		}
	}
	//this checks if the enemy is surrounded by shroud or not
	public boolean uncovered(ArrayList<Point> shroud){
		boolean uncovered = true;
		for(int y = get_location().y-1; y<get_location().y+2;y++){
			for(int x = get_location().x-1; y<get_location().x+2;x++){
				if(shroud.contains(new Point(x,y))){
					uncovered = false;
				}
			}
		}
		return uncovered;
	}
	public int predict_remaining_health(ClassCharacterPlayer player){
		return player.predict_attack_enemy(this);
	}
	public void assign_level_image(){
		switch (level){
			case 1:level_image = "number_01";break;
			case 2:level_image = "number_02";break;
			case 3:level_image = "number_03";break;
			case 4:level_image = "number_04";break;
			case 5:level_image = "number_05";break;
			case 6:level_image = "number_06";break;
			case 7:level_image = "number_07";break;
			case 8:level_image = "number_08";break;
			case 9:level_image = "number_09";break;
			case 10:level_image = "number_10";break;
			default:level_image = "number_01";break;
		}
	}
	public void set_concrete_level(int level, float difficulty){
		difficulty = (float)difficulty/(float)10;
		//ClassLogDisplay.print("Difficulty Enemy: " + difficulty);
		this.level = level;
		assign_level_image();
		update_enemy_stats(difficulty);
	}
	public void set_level(int enemies_placed, float difficulty){
		
		difficulty = (float)difficulty/(float)10;

		if (enemies_placed < 10){
			this.level = 1;
		}else if(enemies_placed < 15){
			this.level = 2;
		}else if(enemies_placed < 19){
			this.level = 3;
		}else if(enemies_placed < 23){
			this.level = 4;
		}else if(enemies_placed < 27){
			this.level = 5;
		}else if(enemies_placed < 30){
			this.level = 6;
		}else if(enemies_placed < 33){
			this.level = 7;
		}else if(enemies_placed < 36){
			this.level = 8;
		}else if(enemies_placed < 39){
			this.level = 9;
		}else{
			this.level = 10;
		}
		assign_level_image();
		update_enemy_stats(difficulty);
	}
	public void set_mods(ClassCharacterModDungeon[] mods){
		attack_mods = new ArrayList<ClassCharacterModDungeon>(Arrays.asList(mods));
		contracted_mods = new ArrayList<ClassCharacterModDungeon>();
		immune_mods = new ArrayList<ClassCharacterModDungeon>();
		for(ClassCharacterModDungeon mod:attack_mods){
			contracted_mods.add(new ClassCharacterModDungeon(mod.get_info_mods(),false));
			immune_mods.add(new ClassCharacterModDungeon(mod.get_info_mods(),false));
		}
		//Init mods here
	}
}